<?

//生成随机地形，存入SESSION
function get_terrain(){
	$t=mt_rand(1,1000);
	if($t <= 300) $_SESSION['FT'] = 0;
	elseif($t <= 500) $_SESSION['FT'] = 1;
	elseif($t <= 700) $_SESSION['FT'] = 2;
	elseif($t <= 800) $_SESSION['FT'] = 3;
	elseif($t <= 900) $_SESSION['FT'] = 4;
	elseif($t <= 970) $_SESSION['FT'] = 5;
	else $_SESSION['FT'] = 6;
}

//用于随机打乱技能ID字符串
function shuffle_string($string) {
	$tmp_array = explode(",",$string);
	shuffle($tmp_array);
	for($i=0;$i<count($tmp_array);$i++) {
		$r .= $tmp_array[$i].",";
	}
	return substr($r,0,-1);
}

//决定指定等级的随机一个技能
function rand_skill($s,$level,$mark=0) {
	$hint = ($mark == 0) ? "变异" : "事故";
	$sarray=array();
	$db=new db_Mysql();
	$rs=$db->execute("select suid,sname,sdesc,slevel,isunique from skill where tomerge=1 and slevel=$level");
	while ($r=$db->fetch_array($rs)) {
		$sarray[] = $r;
	}
	$rands=array_rand($sarray);
	$s->id=$sarray[$rands][0];
	$s->name=$s->name."&nbsp;<font color=red>".$hint."→</font>&nbsp;".$sarray[$rands][1];
	$s->desc=$sarray[$rands][2];
	$s->level=$sarray[$rands][3];
	$s->unique=$sarray[$rands][4];
}

//技能变异处理
function skill_change($s) {
	switch($s->level) {
		case 1:{
			$tmp=mt_rand(1,100);
			if($tmp<=12){rand_skill($s,2);break;}
			elseif($tmp<=19){rand_skill($s,3);break;}
			elseif($tmp==20){rand_skill($s,8);break;}
			else{break;}
		}
		case 2:{
			$tmp=mt_rand(1,100);
			if($tmp<=10){rand_skill($s,3);break;}
			elseif($tmp<=17){rand_skill($s,4);break;}
			elseif($tmp<=20){rand_skill($s,1);break;}
			else{break;}
		}
		case 3:{
			$tmp=mt_rand(1,100);
			if($tmp<=10){rand_skill($s,4);break;}
			elseif($tmp<=16){rand_skill($s,5);break;}
			elseif($tmp<=20){rand_skill($s,2);break;}
			else{break;}
		}
		case 4:{
			$tmp=mt_rand(1,100);
			if($tmp<=7){rand_skill($s,5);break;}
			elseif($tmp<=10){rand_skill($s,6);break;}
			elseif($tmp<=15){rand_skill($s,3);break;}
			else{break;}
		}
		case 5:{
			$tmp=mt_rand(1,100);
			if($tmp<=5){rand_skill($s,6);break;}
			elseif($tmp<=8){rand_skill($s,7);break;}
			elseif($tmp<=15){rand_skill($s,4);break;}
			else{break;}
		}
		case 6:{
			$tmp=mt_rand(1,100);
			if($tmp<=3){rand_skill($s,7);break;}
			elseif($tmp<=8){rand_skill($s,3);break;}
			elseif($tmp<=10){rand_skill($s,2);break;}
			else{break;}
		}
		default:{break;}
	}
	return 0;

}

//技能变化主函数
function post_fusion($skills) {
	switch($_SESSION['FT']) {
		case 6:{$aodds=30;break;}
		case 5:{$aodds=15;break;}
		default :{$aodds=3;}
	}
	//决定是否出现合体事故
	$isacc=($aodds >= mt_rand(1,100)) ? 1 : 0;
	$i=0;
	if($isacc == 0) {
		foreach($skills as $s) {
			skill_change($s);
		}
	} else {
		foreach($skills as $s) {
			if($i<3){
				rand_skill($s,mt_rand(7,8),1);
				$i++;
			} else {
				skill_change($s);
			}
		}
	}

}

?>
